package cn.ekuma.impexp.ui.swing;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.BoxLayout;
import com.openbravo.data.model.Field;
import cn.ekuma.impexp.model.ImpExpTableDefinition;

public class JTableColumnMatchPanel extends JPanel {
	private JTable valueTable;
	private JPanel columnMatchContainer;

	ImpExpTableDefinition tableDefinition;
	ArrayList<JColumnMatchPanel> matchPanels=new ArrayList<JColumnMatchPanel>();
	
	/**
	 * Create the panel.
	 */
	public JTableColumnMatchPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		add(splitPane, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setLeftComponent(scrollPane);
		
		valueTable = new JTable();
		scrollPane.setViewportView(valueTable);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		splitPane.setRightComponent(scrollPane_1);
		
		columnMatchContainer = new JPanel();
		scrollPane_1.setViewportView(columnMatchContainer);
		columnMatchContainer.setLayout(new BoxLayout(columnMatchContainer, BoxLayout.PAGE_AXIS));
	}
	
	public void init(ImpExpTableDefinition tableDefinition){
		this.tableDefinition=tableDefinition;
		Field[] datas=tableDefinition.getBaseTable().getTableFields();
		ColumnMatch columnMatch;
		JColumnMatchPanel columnMatchPanel;
		columnMatchContainer.removeAll();
		matchPanels.clear();
		for(int i=0;i<datas.length;i++){
			columnMatch=new ColumnMatch();
			columnMatch.setSrcCol(i);
			columnMatch.setColName(datas[i].getLabeltran());
			columnMatch.setDataType(datas[i].getData());
			columnMatch.setDestCol(i);
			columnMatchPanel=new JColumnMatchPanel();
			columnMatchPanel.setColumnMatch(columnMatch);
			columnMatchContainer.add(columnMatchPanel);
			matchPanels.add(columnMatchPanel);
		}
		valueTable.setModel(new ImpExpTableDefinitionTableModel(tableDefinition));
	}
	
	public ArrayList<ColumnMatch> execSelectedMatch(){
		ArrayList<ColumnMatch> columnMatchs=new ArrayList<ColumnMatch>();
		ColumnMatch columnMatch;
		for(JColumnMatchPanel panel:matchPanels){
			columnMatch=panel.getColumnMatch();
			if(!columnMatch.isIegore())
			   columnMatchs.add(columnMatch);
		}
		//filterTable(tableDefinition,columnMatchs);
		return columnMatchs;
	}

	/**public void filterTable(ImpExpTableDefinition srcTable,List<ColumnMatch> filter){
		List<ImpExpRowDefinition> rows=srcTable.getDatas();
		Object[] dscDatas;
		ColumnMatch columnMatch;
		int[] filterFieldIndex=new int[filter.size()];
		for(int i=0;i<filter.size();i++){
			filterFieldIndex[i]=filter.get(i).getSrcCol();
		}
		for(ImpExpRowDefinition row:rows){
			dscDatas=new Object[filter.size()];
			Object[] srcDate=row.getDatas();
			for(int i=0;i<filter.size();i++){
				columnMatch=filter.get(i);
				Object srcValue=srcDate[columnMatch.getDestCol()];
				if(columnMatch.getDefValue()!=null)
					if(!columnMatch.isJustChangeNullValue()||columnMatch.isJustChangeNullValue()&&srcValue==null)
					   dscDatas[i]=columnMatch.getDefValue();
					else
						dscDatas[i]=srcValue;
				else if(columnMatch.getFidldGenerator()!=null)
					if(!columnMatch.isJustChangeNullValue()||columnMatch.isJustChangeNullValue()&&srcValue==null)
						   dscDatas[i]=columnMatch.getFidldGenerator().getNext();
						else
							dscDatas[i]=srcValue;
				else
					dscDatas[i]=srcValue;
				row.setDatas(dscDatas);
			}
		}
		srcTable.setFieldIndexs(filterFieldIndex);
	}
*/
	
}
